<?php

/*
 * 取得团购活动列表
 * @return   array
 */
function get_sql_group_buy_list(){	
	/* 过滤条件 */
	$filter['keyword']      	= 	empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
	if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
		$filter['keyword'] 		= 	$filter['keyword'];
	}
	$filter['sort_by']      	= 	empty($_REQUEST['sort_by']) ? 'act_id' : trim($_REQUEST['sort_by']);
	$filter['sort_order']   	= 	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

	$where 						= 	(!empty($filter['keyword'])) ? " AND goods_name LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%'" : '';

	/* 查询 */
	$sql = "SELECT * ".
			"FROM " . 'ybr_goods_activity' .
			" WHERE act_type = '" . GAT_GROUP_BUY . "' $where ".
			" ORDER BY $filter[sort_by] $filter[sort_order] ";
    return $sql;
}

/**
 * 取得品牌列表
 * @return array 品牌列表 id => name
 */
function get_brand_list(){
    $sql 		= 	'SELECT brand_id, brand_name FROM ' . 'ybr_brand' . ' ORDER BY sort_order';
    $res 		= 	M()->query($sql);

    $brand_list = 	array();
    foreach ($res AS $row){
        $brand_list[$row['brand_id']] = addslashes($row['brand_name']);
    }
    return $brand_list;
}

/**
 * 取得商品列表：用于把商品添加到组合、关联类、赠品类
 * @param   object  $filters    过滤条件
 */
function get_goods_list($filter){
    $filter['keyword']	= 	$filter['keyword'];
    $where 				= 	get_where_sql($filter); // 取得过滤条件
    
    /* 取得数据 */
    $sql = 'SELECT goods_id, goods_name, shop_price '.
           'FROM ' . 'ybr_goods' . ' AS g ' . $where .
           'LIMIT 50';    
    //echo $sql;    
    $row 				= 	M()->query($sql);
    return $row;
}


/**
 * 生成过滤条件：用于 get_goodslist 和 get_goods_list
 * @param   object  $filter
 * @return  string
 */
function get_where_sql($filter){
    $time 		= 	date('Y-m-d');

    $where  	= 	isset($filter['is_delete']) && $filter['is_delete'] == '1' ?' WHERE is_delete = 1 ' : ' WHERE is_delete = 0 ';
    $where 		.= 	(isset($filter['real_goods']) && ($filter['real_goods'] > -1)) ? ' AND is_real = ' . intval($filter['real_goods']) : '';
    $where 		.= 	isset($filter['cat_id']) && $filter['cat_id'] > 0 ? ' AND ' . get_children($filter['cat_id']) : '';
    $where 		.= 	isset($filter['brand_id']) && $filter['brand_id'] > 0 ? " AND brand_id = '" . $filter['brand_id'] . "'" : '';
    $where 		.=	isset($filter['intro_type']) && $filter['intro_type'] != '0' ? ' AND ' . $filter['intro_type'] . " = '1'" : '';
    $where 		.= 	isset($filter['intro_type']) && $filter['intro_type'] == 'is_promote' ?
        			" AND promote_start_date <= '$time' AND promote_end_date >= '$time' " : '';
    $where 		.= 	isset($filter['keyword']) && trim($filter['keyword']) != '' ?
        			" AND (goods_name LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%' OR goods_sn LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%' OR goods_id LIKE '%" . Utils::mysql_like_quote($filter['keyword']) . "%') " : '';
    $where 		.= 	isset($filter['suppliers_id']) && trim($filter['suppliers_id']) != '' ?
        			" AND (suppliers_id = '" . $filter['suppliers_id'] . "') " : '';

    $where 		.= 	isset($filter['in_ids']) ? ' AND goods_id ' . db_create_in($filter['in_ids']) : '';
    $where 		.=	isset($filter['exclude']) ? ' AND goods_id NOT ' . db_create_in($filter['exclude']) : '';
    $where 		.= 	isset($filter['stock_warning']) ? ' AND goods_number <= warn_number' : '';
    return $where;
}

/**
 * 取得某商品的团购活动
 * @param   int     $goods_id   商品id
 * @return  array
 */
function goods_group_buy($goods_id){
	$CommonModel	=	new CommonModel();
	
    $sql 			= 	"SELECT * FROM " . 'ybr_goods_activity' .
	           			" WHERE goods_id = '$goods_id' " .
    		       		" AND act_type = '" . GAT_GROUP_BUY . "'" .
            			" AND start_time <= " . gmtime() .
            			" AND end_time >= " . gmtime();
    return $CommonModel->getRow($sql);
}

















